package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxGiveoneChannelModel;
import com.wwdx.service.WwdxGiveoneChannelServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/4/4 10:58
 */
@Service
public class WwdxGiveoneChannelServiceImpl extends BaseServiceImpl<WwdxGiveoneChannelModel> implements WwdxGiveoneChannelServiceI {

    @Override
    public Long selectModelCount(WwdxGiveoneChannelModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "select count(1) from WwdxGiveoneChannelModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxGiveoneChannelModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        String channel = model.getChannel();
        if(StringUtils.isNotBlank(channel)){
            sb.append("and channel=:channel\n");
            params.put("channel",channel);
        }
        return sb.toString();
    }

    @Override
    public List<WwdxGiveoneChannelModel> selectModelList(WwdxGiveoneChannelModel model, Integer page, Integer rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String filed = model.getSort();
        String sort = "date";
        if (StringUtils.isNotBlank(filed)){
            sort = filed;
        }
        String hql = "from WwdxGiveoneChannelModel\t"+this.getParams(model, params)+"\torder by "+sort+" "+model.getSortOrder()+"";
        return find(hql, params, page, rows);
    }
}